package com.zj.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seventh.common.pojo.Payload;
import com.seventh.common.pojo.R;
import com.seventh.common.utils.JwtUtils;
import com.zj.system.config.RsaKeyProperties;
import com.zj.system.mapper.RoleMapper;
import com.zj.system.pojo.entity.Role;

import com.zj.system.pojo.entity.User;
import com.zj.system.pojo.vo.RoleVo;
import com.zj.system.service.RoleService;
import com.zj.system.utils.WebUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: clms-seventh
 * @description: 角色分配业务逻辑层
 * @author: 张静
 * @create: 2021-07-19 19:37
 **/
@Service
public class RoleServiceImpl implements RoleService {
    @Resource
    private RoleMapper roleMapper;
    @Autowired
    private RsaKeyProperties rsaKeyProperties;
    @Override
    public R selRole(Integer userId) {
        List<Role> roles = roleMapper.selectList(null);
        List<Role> userRoles=roleMapper.selByUserId(userId);
        List<Integer> userRole=new ArrayList<>();
        for(Role role:userRoles){
            userRole.add(role.getRoleId());
        }
        return R.ok().put("roleAll",roles).put("userRole",userRole);
    }
    @Transactional
    @Override
    public R upUserRole(Integer userId, List<Integer> roles) {
        roleMapper.deleteByUserId(userId);
        roleMapper.insertByUserId(userId,roles);
        return R.ok();
    }
    @Override
    public R sel(Integer pageSize, Integer currentPage, RoleVo vo) {
        Page<Role> page=new Page<>(currentPage,pageSize);
        QueryWrapper<Role> wrapper=new QueryWrapper<>();
        if (StringUtils.isNotEmpty(vo.getRoleName())){
            wrapper.like("role_name",vo.getRoleName());
        }
        if(StringUtils.isNotEmpty(vo.getStartDate())){
            wrapper.ge("create_time",vo.getStartDate());
        }
        if(StringUtils.isNotEmpty(vo.getEndDate())){
            wrapper.le("create_time",vo.getEndDate());
        }
        roleMapper.selectPage(page,wrapper);
        return R.ok().put("tableData",page);
    }

    @Override
    public R upStatus(Integer roleId, Integer status) {
        UpdateWrapper<Role> wrapper=new UpdateWrapper();
        wrapper.eq("role_id",roleId);
        wrapper.set("status",status);
        roleMapper.update(null,wrapper);
        return R.ok();
    }

    @Override
    public R ins(Role role) {
        User useUser = this.getToken();
        role.setCreateBy(useUser.getUserName());
        role.setCreateTime(LocalDateTime.now());
        roleMapper.insert(role);
        return R.ok().put("message","添加成功");
    }

    @Override
    public R up(Role role) {
        User useUser = this.getToken();
        UpdateWrapper<Role> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("role_name",role.getRoleName());
        updateWrapper.eq("role_id",role.getRoleId());
        updateWrapper.set("update_by",useUser.getUserName());
        updateWrapper.set("update_time",LocalDateTime.now());
        roleMapper.update(null,updateWrapper);
        return R.ok().put("message","修改成功");
    }

    @Override
    public R del(Integer roleId) {
        roleMapper.deleteById(roleId);
        return R.ok().put("message","删除成功");
    }

    @Override
    public R selRoleName(Role role) {
        List<Role> list=roleMapper.selectList(null);
        int result=0;
        for (Role u:list) {
            if (role.getRoleId()!=null){
                if (!u.getRoleId().equals(role.getRoleId())){
                    if (u.getRoleName().equals(role.getRoleName())){
                        result=1;
                        break;
                    }
                }
            }else {
                if (u.getRoleName().equals(role.getRoleName())){
                    result=1;
                    break;
                }
            }
        }
        return R.ok().put("code",result);
    }

    @Override
    public R selById(Integer roleId) {
        Role role = roleMapper.selectById(roleId);
        return R.ok().put("roleInfo",role);
    }

    private User getToken(){
        HttpServletRequest request= WebUtil.getRequest();
        String token = request.getHeader("token");
        Payload<User> info = JwtUtils.getInfoFromToken(token, rsaKeyProperties.getPublicKey(), User.class);
        return info.getUserInfo();
    }


}
